home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / bb_tree1.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  5.9 KB  |  251 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  bb_tree1.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15.  
  16.  
  17. #ifndef LEDA_BB_TREE_H
  18. #define LEDA_BB_TREE_H
  19.  
  20. //--------------------------------------------------------------------
  21. //  
  22. //  BB[alpha] Trees
  23. //
  24. //  Michael Wenzel   (1989)
  25. //
  26. // Implementation follows
  27. // Kurt Mehlhorn: Data Structures and Algorithms 1, section III.5.1
  28. //
  29. // Aenderungen:
  30. //   - virtuelle compare-Funktion   (M. Wenzel, Nov. 1989)
  31. //--------------------------------------------------------------------
  32.  
  33.  
  34. #include <LEDA/b_stack.h>
  35.  
  36.  
  37. // -----------------------------------------------------
  38. // declarations and definitions
  39. // -------------------------------------------------------
  40.  
  41. const int BSTACKSIZE = 32 ; // according to tree.size and alpha
  42.  
  43. const float SQRT1_2 = 0.70710678;
  44.  
  45. class bb_node;
  46. class bb_tree;
  47.  
  48. typedef bb_node* bb_item;
  49.  
  50. typedef bb_node* bb_tree_item;
  51.  
  52. typedef b_stack<bb_item> bb_tree_stack;
  53.  
  54.  
  55. typedef void (*DRAW_BB_NODE_FCT)(double,double,void*);
  56. typedef void (*DRAW_BB_EDGE_FCT)(double,double,double,double);
  57.  
  58. class bb_node {
  59.  
  60.   GenPtr ke;
  61.   GenPtr inf;
  62.   int gr;
  63.   bb_item sohn[2];
  64.  
  65.   friend class bb_tree;
  66.   friend class range_tree;
  67.   friend class Segment_Tree;
  68.   friend class seg_node_tree;
  69.  
  70. public:
  71.  
  72.   GenPtr key()           { return ke; }
  73.   GenPtr info()          { return inf; }
  74.  
  75.   int blatt()         { return (gr==1); }
  76.   int groesse()       { return gr; }
  77.  
  78.   float bal()
  79.     { if (blatt()) return 0.5 ;
  80.       else return float(float(sohn[0]->groesse())/float(gr));
  81.         }
  82.  
  83.   bb_node(GenPtr k=0,GenPtr i=0,int leaf=0,bb_item ls=0,bb_item rs=0)
  84.     { ke = k;
  85.       inf = i;
  86.       sohn[0] = ls;
  87.       sohn[1] = rs;
  88.       if (leaf==0)
  89.     gr=1;
  90.       else gr = ls->groesse()+rs->groesse();
  91.     }
  92.  
  93.   bb_node(bb_item p)
  94.     { 
  95.       ke = p->key();
  96.       inf = p->info();
  97.       gr = p->groesse();
  98.       sohn[0] = p->sohn[0];
  99.       sohn[1] = p->sohn[1];
  100.     }
  101.       
  102.   LEDA_MEMORY(bb_node)
  103.  
  104. }; 
  105.  
  106.  
  107.  
  108. class bb_tree {
  109.  
  110.   bb_item root;
  111.   bb_item first;
  112.   bb_item iterator;
  113.   int   anzahl; 
  114.   float alpha;
  115.   float d;
  116.   bb_tree_stack st;
  117.  
  118.   friend class bb_node;
  119.   friend class range_tree;
  120.   friend class seg_node_tree;
  121.   friend class Segment_Tree;
  122.  
  123.   int   blatt(bb_item it)
  124.     { return (it) ? it->blatt() : 0; }
  125.  
  126.   void  lrot(bb_item , bb_item ); 
  127.   void  rrot(bb_item , bb_item ); 
  128.   void  ldrot(bb_item , bb_item ); 
  129.   void  rdrot(bb_item , bb_item ); 
  130.  
  131.   void  deltree(bb_item);
  132.   bb_item copytree(bb_item , bb_item , bb_item& );
  133.   bb_item search(GenPtr);
  134.   bb_item sinsert(GenPtr , GenPtr );
  135.   bb_item sdel(GenPtr );
  136.  
  137. protected:
  138.  bb_tree_item item(void* p) const { return bb_tree_item(p); }
  139.  
  140.  
  141. public:
  142.  
  143.   virtual int cmp(GenPtr x, GenPtr y) const { return compare(x,y); }
  144.   virtual void clear_key(GenPtr&) const {}
  145.   virtual void clear_inf(GenPtr&) const {}
  146.   virtual void copy_key(GenPtr&)  const {}
  147.   virtual void copy_inf(GenPtr&)  const {}
  148.  
  149.   GenPtr     key(bb_item it)  const   { return it->ke;  }
  150.   GenPtr&    info(bb_item it)         { return it->inf; }
  151.   GenPtr     inf(bb_item it)  const   { return it->inf; }
  152.   GenPtr     translate(GenPtr y);
  153.  
  154.   bb_item insert(GenPtr ,GenPtr );
  155.   bb_item change_obj(GenPtr ,GenPtr );
  156.   bb_item change_inf(bb_item it,GenPtr y) { if (it)  
  157.                                        { it->inf = y;
  158.                                            return it; }
  159.                                          else return 0;
  160.                                         }
  161.  
  162.   bb_item del(GenPtr);
  163.  
  164.   void del_item(bb_item it) { if (it) del(it->key()); }
  165.   void del_min() { if (first) del(first->key()); } 
  166.   void decrease_key(bb_item p, GenPtr k) { GenPtr i = p->info(); 
  167.                                             del(p->key());
  168.                                             insert(k,i);
  169.                                            }
  170.  
  171.   bb_item locate(GenPtr)  const;
  172.   bb_item located(GenPtr) const;
  173.   bb_item lookup(GenPtr)  const;
  174.  
  175.   bb_item cyclic_succ(bb_item it)  const
  176.         { return it ? it->sohn[1] : 0 ; }
  177.   bb_item cyclic_pred(bb_item it)  const
  178.       { return it ? it->sohn[0] : 0 ; }
  179.   bb_item succ(bb_item it) const
  180.           { return ( it && it->sohn[1] != first ) ? it->sohn[1] : 0 ; }
  181.   bb_item pred(bb_item it) const
  182.           { return ( it && it != first ) ? it->sohn[0] : 0 ; }
  183.  
  184.   bb_item ord(int k);
  185.   bb_item min()       const    { return (first) ? first : 0; }
  186.   bb_item find_min()  const    { return (first) ? first : 0; }
  187.   bb_item max()       const    { return (first) ? first->sohn[0] : 0 ; }
  188.  
  189.   bb_item first_item() const         { return (first) ? first : 0; }
  190.   bb_item next_item(bb_item x) const { return succ(x); }
  191.  
  192.   bb_item move_iterator() ;
  193.  
  194.   int current_item(bb_item& x) 
  195.      { if (!iterator) return 0;
  196.        else { x = iterator; return 1; }
  197.      }
  198.  
  199.  
  200.   void init_iterator()          { iterator = 0; }
  201.   void  lbreak()                { iterator = 0; }
  202.  
  203.  
  204.   void  clear();
  205.   int   size()    const        { return anzahl; }
  206.   int   empty()   const        { return (anzahl==0) ? true : false; }
  207.  
  208.   int   member(GenPtr );
  209.   bb_tree& operator=(const bb_tree& w);
  210.  
  211.   void  set_alpha(float a) 
  212.         { if (anzahl>=3)
  213.              error_handler(4,"aenderung von alpha nicht erlaubt");
  214.           if ((a<0.25) || (a>1-SQRT1_2))
  215.              error_handler(3,"alpha not in range");
  216.           alpha=a;
  217.           d = 1/(2-alpha) ;
  218.         }
  219.  
  220.   float get_alpha() { return alpha; }
  221.  
  222.   bb_tree() : st(BSTACKSIZE)
  223.   { 
  224.     root = first = iterator = 0;
  225.     anzahl = 0;
  226.     alpha=0.28;
  227.     d=1/(2-alpha);
  228.   }
  229.  
  230.   bb_tree(float);
  231.   bb_tree(const bb_tree&);
  232.  
  233.  
  234. // virtual  // if the destructor is declared virtual range/segment trees crash
  235.  
  236.   ~bb_tree()  { clear(); }
  237.  
  238.  
  239.  
  240.   void draw(DRAW_BB_NODE_FCT, DRAW_BB_EDGE_FCT, bb_node*,
  241.             double, double, double, double, double);
  242.  
  243.   void draw(DRAW_BB_NODE_FCT, DRAW_BB_EDGE_FCT, 
  244.             double, double, double, double);
  245.  
  246. };
  247.  
  248.  
  249. #endif
  250.